package com.andreas.shop.controller;

import com.andreas.shop.common.ApiRestResponse;
import com.andreas.shop.exception.ShopBussinessException;
import com.andreas.shop.pojo.request.CreateOrderRequest;
import com.andreas.shop.pojo.vo.OrderVO;
import com.andreas.shop.service.OrderService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import net.bytebuddy.asm.Advice;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author andreaszhou
 * @ClassName OrderController
 * @Description orderController
 * @date 2021/2/2 8:45
 * @Version 1.0
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderServiceImp;

    @ApiOperation("用户创建订单")
    @PostMapping("/create")
    @ResponseBody
    /*记住这里我们是要传入的是requestBody，不然我们的程序会显示 Required CreateOrderRequest parameter 'createOrderRequest' is not present*/
    public ApiRestResponse createOrder(@RequestBody CreateOrderRequest createOrderRequest) {
        String orderNo;
        try {
            orderNo = orderServiceImp.createOrder(createOrderRequest);
        } catch (ShopBussinessException e) {
            e.printStackTrace();
            return ApiRestResponse.error(e.getCode(), e.getMessage());
        }
        return ApiRestResponse.success(orderNo);
    }


    @ApiOperation("用户显示订单详情")
    @GetMapping("/detail")
    @ResponseBody
    public ApiRestResponse orderDetail(@RequestParam("orderNo") String orderNo) {
        OrderVO orderVO;
        try {
            orderVO = orderServiceImp.orderDetail(orderNo);
        } catch (ShopBussinessException e) {
            e.printStackTrace();
            return ApiRestResponse.error(e.getCode(), e.getMessage());
        }
        return ApiRestResponse.success(orderVO);
    }


    @ApiOperation("用户订单列表")
    @ResponseBody
    @GetMapping("/list")
    public ApiRestResponse orderList(Integer pageNum,Integer pageSize){
        PageInfo pageInfo = orderServiceImp.orderList(pageNum, pageSize);
        return ApiRestResponse.success(pageInfo);
    }

    @ApiOperation("用户取消订单")
    @ResponseBody
    @PostMapping("/cancel")
    public ApiRestResponse cancelOrder(@RequestParam("orderNo") String orderNo){
        try {
            orderServiceImp.cancelOrder(orderNo);
        } catch (ShopBussinessException e) {
            e.printStackTrace();
            return ApiRestResponse.error(e.getCode(),e.getMessage());
        }
        return ApiRestResponse.success();
    }

    @ApiOperation("生成支付二维码")
    @ResponseBody
    @PostMapping("/qrcode")
    public ApiRestResponse qrCode(@RequestParam("orderNo") String orderNo){
        String qrcode = orderServiceImp.qrcode(orderNo);
        return ApiRestResponse.success(qrcode);
    }

    @GetMapping("pay")
    @ApiOperation("支付接口")
    @ResponseBody
    public ApiRestResponse pay(@RequestParam("orderNo") String orderNo) {
        try {
            orderServiceImp.pay(orderNo);
        } catch (ShopBussinessException e) {
            e.printStackTrace();
            return ApiRestResponse.error(e.getCode(),e.getMessage());
        }
        return ApiRestResponse.success();
    }

    @PostMapping("/finish")
    @ResponseBody
    @ApiOperation("订单完结-用户和管理员")
    public ApiRestResponse finish(@RequestParam("orderNo") String orderNo){
        try {
            orderServiceImp.finish(orderNo);
        } catch (ShopBussinessException e) {
            e.printStackTrace();
            return ApiRestResponse.error(e.getCode(),e.getMessage());
        }
        return ApiRestResponse.success();
    }
}
